import { takeEvery } from "redux-saga/effects";
import { NATIVE_CLASS } from "@common/constants";
import { callNativeFunc } from "@common/saga-helper";
import {
  SAGA_TMP_PALLET_LOAD_OPS_FORM,
  SAGA_TMP_PALLET_LOAD_ITEM_DETAILS,
  SAGA_TMP_PALLET_LOAD_BARCODE_DETAILS,
  SAGA_TMP_PALLET_STASH_OPS_DATA,
  SAGA_TMP_PALLET_FINISH_OPS,
  SAGA_TMP_PALLET_DEL_BARCODE,
  SAGA_TMP_PALLET_PUSH_2_SERVER
} from "./saga-types";
import {
  SET_TMP_PALLET_OPS_FORM,
  SET_TMP_PALLET_ITEM_DETAILS,
  SET_TMP_PALLET_BARCODE_DETAILS,
  DEL_TMP_PALLET_BARCODE
} from "./action-types";

function* loadTmpPalletOpsFormSaga() {
  yield takeEvery(SAGA_TMP_PALLET_LOAD_OPS_FORM, function* (action) {
    yield* callNativeFunc({
      effects: [
        {
          payload: {
            classFullName: NATIVE_CLASS.TmpPalletOpsHandler,
            action: "loadOpsFormData",
            needNonce: false,
            params: action.payload,
          },
          respActions: [SET_TMP_PALLET_OPS_FORM],
        },
      ],
    });
  });
}

function* loadTmpPalletItemDetailsSaga() {
  yield takeEvery(SAGA_TMP_PALLET_LOAD_ITEM_DETAILS, function* (action) {
    yield* callNativeFunc({
      effects: [
        {
          payload: {
            classFullName: NATIVE_CLASS.TmpPalletDetailsHandler,
            action: "loadItemDetails",
            needNonce: false,
            params: action.payload,
          },
          respActions: [SET_TMP_PALLET_ITEM_DETAILS],
        },
      ],
    });
  });
}

function* loadTmpPalletBarcodeDetailsSaga() {
  yield takeEvery(SAGA_TMP_PALLET_LOAD_BARCODE_DETAILS, function* (action) {
    yield* callNativeFunc({
      effects: [
        {
          payload: {
            classFullName: NATIVE_CLASS.TmpPalletDetailsHandler,
            action: "loadBarcodeDetails",
            needNonce: false,
            params: action.payload,
          },
          respActions: [SET_TMP_PALLET_BARCODE_DETAILS],
        },
      ],
    });
  });
}

function* stashTmpPalletOpsDataSaga() {
  yield takeEvery(SAGA_TMP_PALLET_STASH_OPS_DATA, function* (action) {
    yield* callNativeFunc({
      effects: [
        {
          payload: {
            classFullName: NATIVE_CLASS.TmpPalletOpsHandler,
            action: "create",
            needNonce: false,
            params: action.payload,
          },
          callback: action.payload.callback,
        },
        {
          buildPayload: (resps) => ({
            classFullName: NATIVE_CLASS.TmpPalletOpsHandler,
            action: "loadOpsFormData",
            needNonce: false,
            params: {id: resps[0].payload.id}
          }),
          respActions: [SET_TMP_PALLET_OPS_FORM],
        },
      ],
    });
  });
}

function* finishTmpPalletOpsSaga() {
  yield takeEvery(SAGA_TMP_PALLET_FINISH_OPS, function* (action) {
    yield* callNativeFunc({
      effects: [
        {
          payload: {
            classFullName: NATIVE_CLASS.TmpPalletOpsHandler,
            action: "finish",
            needNonce: false,
            params: action.payload,
          },
          callback: action.payload.callback,
        },
      ],
    });
  });
}

function* delTmpPalletBarcodeSaga() {
  yield takeEvery(SAGA_TMP_PALLET_DEL_BARCODE, function* (action) {
    yield* callNativeFunc({
      effects: [
        {
          payload: {
            classFullName: NATIVE_CLASS.TmpPalletOpsHandler,
            action: "delBarcode",
            needNonce: false,
            params: action.payload,
          },
          respActions: [DEL_TMP_PALLET_BARCODE],
          callback: action.payload.callback
        },
      ],
    });
  });
}

function* pushTmpPallet2ServerSaga() {
  yield takeEvery(SAGA_TMP_PALLET_PUSH_2_SERVER, function* (action) {
    yield* callNativeFunc({
      effects: [
        {
          payload: {
            classFullName: NATIVE_CLASS.TmpPalletHandler,
            action: "push2Server",
            needNonce: true,
            params: action.payload,
          },
          callback: action.payload.callback
        },
      ],
    });
  });
}

export default [
  loadTmpPalletOpsFormSaga(),
  loadTmpPalletItemDetailsSaga(),
  loadTmpPalletBarcodeDetailsSaga(),
  stashTmpPalletOpsDataSaga(),
  finishTmpPalletOpsSaga(),
  delTmpPalletBarcodeSaga(),
  pushTmpPallet2ServerSaga()
];
